#pragma once
#include "uart/ProtocolSender.h"

#include "logic/global_variables.h"
#include "water/timer_event.h"
#include "logic_utility.hpp"




//// 将时间（小时和分钟）转换为秒数
//int timeToSeconds(int hours, int minutes) {
//    if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59) {
//
//        return -1; // 返回 -1 表示错误
//    }
//    return hours * 3600 + minutes * 60;
//}
//
//// 将秒数转换为对应的时间（小时和分钟）
//void secondsToTime(int seconds, int& hours, int& minutes) {
//    if (seconds < 0 || seconds >= 24 * 3600) {
//        hours = -1;
//        minutes = -1;
//        return;
//    }
//    hours = seconds / 3600;          // 计算小时
//    minutes = (seconds % 3600) / 60; // 计算剩余的分钟
//}
void buttonClean(bool on){
    minternal_cleaningPtr->setSelected(on);
    mforce_defrostPtr->setInvalid(on);
}
void buttonForce(bool on){
	mforce_defrostPtr->setSelected(on);
	minternal_cleaningPtr->setInvalid(on);
}

//设置列表自定义时间
static void ListTime(){
		 mhourList0Ptr->setSelection(Global::Timer::iceHL == 0?23:Global::Timer::iceHL-1);
		 mminList0Ptr->setSelection(Global::Timer::iceML ==0? 59:Global::Timer::iceML-1);
		 mhourList1Ptr->setSelection(Global::Timer::iceHR == 0?23:Global::Timer::iceHR-1);
		 mminList1Ptr->setSelection(Global::Timer::iceMR ==0? 59:Global::Timer::iceMR-1);

		char buf [36]={0};
		snprintf(buf,sizeof(buf),"%02d:%02d-%02d:%02d",Global::Timer::iceHL, Global::Timer::iceML,
				Global::Timer::iceHR, Global::Timer::iceMR);
		 mTextTimePtr->setText(buf );
}

/**
 * 注册定时器
 * 填充数组用于注册定时器
 * 注意：id不能重复
 */
static S_ACTIVITY_TIMEER REGISTER_ACTIVITY_TIMER_TAB[] = {
	//{0,  6000}, //定时器id=0, 时间间隔6秒
	{1,  300},
};

static int8_t flagTime = 0;
/**
 * 当界面构造时触发
 */
static void onUI_init(){
    //Tips :添加 UI初始化的显示代码到这里,如:mText1Ptr->setText("123");
	miceSwitchPtr->setSelected(isIceOpen());
    mshortcutSwitchPtr->setSelected(Global::Flags::shortcutOut);
	miceSetPtr->setSelected(true);
	mshortcutWndPtr->hideWnd();
	miceSetWndPtr->showWnd();
	switch(Global::Timer::singleice){
		case TIME_POINT_600:{
			micesizePtr->setText("小");
			flagTime = 0;
		}break;
		case TIME_POINT_900:{
			micesizePtr->setText("中");
			flagTime = 1;
		}break;
		case TIME_POINT_1200:{
			micesizePtr->setText("大");
			flagTime = 2;
		}break;
	}

	if(Global::Flags::custom_ice){
		mcustomSwitchPtr->setSelected(true);
	}else{
		mallDayPtr->setSelected(true);
	    mButtonDPtr->setInvalid(true);
	}
	ListTime();

    if(Global::Flags::shortcutLong){
    	mButtonLongPtr->setSelected(true);
    }else{
    	mButtonSustainPtr->setSelected(true);
    }

    if(Global::Flags::forceDefrost){
    	mforce_defrostPtr->setSelected(true);
    	minternal_cleaningPtr->setInvalid(true);
    }else if(Global::Flags::internalCleaning){
        minternal_cleaningPtr->setSelected(true);
        mforce_defrostPtr->setInvalid(true);
    }


}

/**
 * 当切换到该界面时触发
 */
static void onUI_intent(const Intent *intentPtr) {
    if (intentPtr != NULL) {
        //TODO
    }
}

/*
 * 当界面显示时触发
 */
static void onUI_show() {
	mTextView4Ptr->setTouchPass(true);
	mTextView9Ptr->setTouchPass(true);
}

/*
 * 当界面隐藏时触发
 */
static void onUI_hide() {

}

/*
 * 当界面完全退出时触发
 */
static void onUI_quit() {

}

/**
 * 串口数据回调接口
 */
static void onProtocolDataUpdate(const SProtocolData &data) {

}

/**
 * 定时器触发函数
 * 不建议在此函数中写耗时操作，否则将影响UI刷新
 * 参数： id
 *         当前所触发定时器的id，与注册时的id相同
 * 返回值: true
 *             继续运行当前定时器
 *         false
 *             停止运行当前定时器
 */
static bool onUI_Timer(int id){
	switch (id) {
	case 1:{
		miceSwitchPtr->setSelected(isIceOpen());
	}break;
		default:
			break;
	}
    return true;
}

/**
 * 有新的触摸事件时触发
 * 参数：ev
 *         新的触摸事件
 * 返回值：true
 *            表示该触摸事件在此被拦截，系统不再将此触摸事件传递到控件上
 *         false
 *            触摸事件将继续传递到控件上
 */
static bool oniceMakingActivityTouchEvent(const MotionEvent &ev) {
    switch (ev.mActionStatus) {
		case MotionEvent::E_ACTION_DOWN://触摸按下
			//LOGD("时刻 = %ld 坐标  x = %d, y = %d", ev.mEventTime, ev.mX, ev.mY);
			break;
		case MotionEvent::E_ACTION_MOVE://触摸滑动
			break;
		case MotionEvent::E_ACTION_UP:  //触摸抬起
			break;
		default:
			break;
	}
	return false;
}
static bool onButtonClick_customSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick customSwitch !!!\n");
    mallDayPtr->setSelected(false);
    pButton->setSelected(true);
    mButtonDPtr->setInvalid(false);
    return false;
}

static bool onButtonClick_allDay(ZKButton *pButton) {
    LOGD(" ButtonClick allDay !!!\n");
    pButton->setSelected(true);
    mcustomSwitchPtr->setSelected(false);
    mButtonDPtr->setInvalid(true);
    return false;
}

static bool onButtonClick_ButtonD(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonD !!!\n");
    mtimeWndPtr->showWnd();
    return false;
}

static bool onButtonClick_ButtonL(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonL !!!\n");
    flagTime--;
    if(flagTime < 0)flagTime = 2;
	switch(flagTime){
		case 0:{
			micesizePtr->setText("小");
		}break;
		case 1:{
			micesizePtr->setText("中");
		}break;
		case 2:{
			micesizePtr->setText("大");
		}break;
	}
    return false;
}

static bool onButtonClick_ButtonR(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonR !!!\n");
    flagTime = (flagTime + 1) % 3;
	switch(flagTime){
		case 0:{
			micesizePtr->setText("小");
		}break;
		case 1:{
			micesizePtr->setText("中");
		}break;
		case 2:{
			micesizePtr->setText("大");
		}break;
	}
    return false;
}



static bool onButtonClick_cancel(ZKButton *pButton) {
    LOGD(" ButtonClick cancel !!!\n");
    EASYUICONTEXT->goBack();
    return false;
}

static bool onButtonClick_iceSet(ZKButton *pButton) {
    LOGD(" ButtonClick iceSet !!!\n");
    miceSetPtr->setSelected(true);
    mshortcutSetPtr->setSelected(false);
    miceSetWndPtr->showWnd();
    mshortcutWndPtr->hideWnd();
    return false;
}

static bool onButtonClick_iceSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick iceSwitch !!!\n");
    operateIce(!isIceOpen());
    miceSwitchPtr->setSelected(isIceOpen());
    miceSetPtr->setSelected(true);
    mshortcutSetPtr->setSelected(false);
    miceSetWndPtr->showWnd();
    mshortcutWndPtr->hideWnd();
    return false;
}

static bool onButtonClick_shortcutSet(ZKButton *pButton) {
    LOGD(" ButtonClick shortcutSet !!!\n");
    mshortcutSetPtr->setSelected(true);
    miceSetPtr->setSelected(false);
    miceSetWndPtr->hideWnd();
    mshortcutWndPtr->showWnd();

    return false;
}

static bool onButtonClick_shortcutSwitch(ZKButton *pButton) {
    LOGD(" ButtonClick shortcutSwitch !!!\n");
    pButton->setSelected(!pButton->isSelected());
    mshortcutSetPtr->setSelected(true);
    miceSetPtr->setSelected(false);
    miceSetWndPtr->hideWnd();
    mshortcutWndPtr->showWnd();

    Global::Flags::shortcutOut =mshortcutSwitchPtr->isSelected();
    return false;
}
static bool onButtonClick_sys_back(ZKButton *pButton) {
    LOGD(" ButtonClick sys_back !!!\n");
    return false;
}
static bool onButtonClick_timeOk(ZKButton *pButton) {
    LOGD(" ButtonClick timeOk !!!\n");
    Global::Timer::iceHL = ( (mhourList0Ptr->getFirstVisibleItemIndex()+1)% mhourList0Ptr->getListItemCount() );
    Global::Timer::iceHR = ( (mhourList1Ptr->getFirstVisibleItemIndex()+1)% mhourList1Ptr->getListItemCount() );
    Global::Timer::iceML = ( (mminList0Ptr->getFirstVisibleItemIndex()+1)% mminList0Ptr->getListItemCount() );
    Global::Timer::iceMR = ( (mminList1Ptr->getFirstVisibleItemIndex()+1)% mminList1Ptr->getListItemCount() );
    ListTime();
    mtimeWndPtr->hideWnd();
    return false;
}

static bool onButtonClick_timeCancel(ZKButton *pButton) {
    LOGD(" ButtonClick timeCancel !!!\n");
    mtimeWndPtr->hideWnd();
    return false;
}

static int getListItemCount_hourList0(const ZKListView *pListView) {
    //LOGD("getListItemCount_hourList0 !\n");
    return 24;
}

static void obtainListItemData_hourList0(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ hourList0  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d时",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);

}

static void onListItemClick_hourList0(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ hourList0  !!!\n");
}

static int getListItemCount_minList0(const ZKListView *pListView) {
    //LOGD("getListItemCount_minList0 !\n");
    return 60;
}

static void obtainListItemData_minList0(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ minList0  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d分",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);
}

static void onListItemClick_minList0(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ minList0  !!!\n");
}

static int getListItemCount_hourList1(const ZKListView *pListView) {
    //LOGD("getListItemCount_hourList1 !\n");
    return 24;
}

static void obtainListItemData_hourList1(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ hourList1  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d时",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);
}

static void onListItemClick_hourList1(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ hourList1  !!!\n");
}

static int getListItemCount_minList1(const ZKListView *pListView) {
    //LOGD("getListItemCount_minList1 !\n");
    return 60;
}

static void obtainListItemData_minList1(ZKListView *pListView,ZKListView::ZKListItem *pListItem, int index) {
    //LOGD(" obtainListItemData_ minList1  !!!\n");
	char buf [10]={0};
	snprintf(buf,sizeof(buf),"%02d分",index);
	pListItem->setText(buf);
	pListItem->setTextColor(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()?  0xFFFFFF : 0x474D50);
	pListItem->setTextSize(index == (pListView->getFirstVisibleItemIndex()+1)%pListView->getListItemCount()? 34 : 28);
}

static void onListItemClick_minList1(ZKListView *pListView, int index, int id) {
    //LOGD(" onListItemClick_ minList1  !!!\n");
}

static bool onButtonClick_ButtonSustain(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonSustain !!!\n");
	mButtonLongPtr->setSelected(false);
	mButtonSustainPtr->setSelected(true);
    return false;
}

static bool onButtonClick_ButtonLong(ZKButton *pButton) {
    LOGD(" ButtonClick ButtonLong !!!\n");
	mButtonLongPtr->setSelected(true);
	mButtonSustainPtr->setSelected(false);
    return false;
}

static bool onButtonClick_ok(ZKButton *pButton) {
    LOGD(" ButtonClick ok !!!\n");
    if(miceSetWndPtr->isWndShow()){
    	switch(flagTime){
    		case 0:{
    			Global::Timer::singleice = TIME_POINT_600;
    			Global::j_handler.saveInt("icesize", TIME_POINT_600);
    		}break;
    		case 1:{
    			Global::Timer::singleice = TIME_POINT_900;
    			Global::j_handler.saveInt("icesize", TIME_POINT_900);
    		}break;
    		case 2:{
    			Global::Timer::singleice = TIME_POINT_1200;
    			Global::j_handler.saveInt("icesize", TIME_POINT_1200);
    		}break;
    	}
    	if(mcustomSwitchPtr->isSelected()){
    		Global::Flags::custom_ice = true;
    		Global::j_handler.saveInt("starticeTH", Global::Timer::iceHL);
    		Global::j_handler.saveInt("starticeTM", Global::Timer::iceML);

    		Global::j_handler.saveInt("endiceTH", Global::Timer::iceHR);
    		Global::j_handler.saveInt("endiceTM", Global::Timer::iceMR);
    	}else{
    		Global::Flags::custom_ice = false;
    	}
    	Global::j_handler.saveBool("customice", Global::Flags::custom_ice);
    }else{
    	if(mButtonLongPtr->isSelected()){
    		Global::Flags::shortcutLong = true;
    	}else{
    		Global::Flags::shortcutLong = false;
    	}
        Global::Flags::shortcutOut =mshortcutSwitchPtr->isSelected();
    }
    EASYUICONTEXT->goBack();
    return false;
}
static bool onButtonClick_force_defrost(ZKButton *pButton) {
    LOGD(" ButtonClick force_defrost !!!\n");
    pButton->setSelected(!pButton->isSelected());
    forceDefrost(pButton->isSelected());
	minternal_cleaningPtr->setInvalid(pButton->isSelected());
    return false;
}

static bool onButtonClick_internal_cleaning(ZKButton *pButton) {
    LOGD(" ButtonClick internal_cleaning !!!\n");
    pButton->setSelected(!pButton->isSelected());
    internalClean(pButton->isSelected());
    mforce_defrostPtr->setInvalid(pButton->isSelected());
    return false;
}
